import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class LoadingPage extends StatefulWidget {
  @override
  _LoadingPageState createState() => _LoadingPageState();
}
class _LoadingPageState extends State<LoadingPage> {
  Timer _timer;
  int _countdownTime = 3;
  @override
  void initState() {
    super.initState();
//    Future.delayed(Duration(seconds: 3), () {
//      print('停顿3秒------');
//      Navigator.push(context, MaterialPageRoute(builder: (context)=> TabNatigator()));
//    });
    startCountdownTimer();
  }
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Stack(
        children: <Widget>[
          Image.asset('assets/loading.jpg', fit: BoxFit.fill),
          Container(
            child: Align(
              child: Container(
                padding: EdgeInsets.only(top: 30, right: 30),
                alignment: Alignment.topRight,
                child: Container( 
                  width: 80,
                  height: 30,
                  decoration: BoxDecoration(color: Colors.black54, borderRadius: BorderRadius.circular(20)),
                  child: Center(
                    child: FlatButton(
                      onPressed: () {
//                        Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => TabNatigator()));
                        Navigator.pushReplacementNamed(context, 'tab_natigator');
                      },
                      child: Text('跳过${_countdownTime }', style: TextStyle(fontSize: 14, color: Colors.black54, decoration: TextDecoration.none)),
                    ),
                  )
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
  void startCountdownTimer() {
    const oneSec = const Duration(seconds: 1);
    var callback = (timer) {
      setState(() {
        if (_countdownTime <= 1) {
          Navigator.pushReplacementNamed(context, 'tab_natigator');
//          Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => TabNatigator()));
          _timer.cancel();
        } else {
          _countdownTime = _countdownTime - 1;
        }
      });
    };

    _timer = Timer.periodic(oneSec, callback);
  }
  @override
  void dispose() {
    if (_timer != null) {
      _timer.cancel();
    }
    super.dispose();
  }
}